package com.dq.service.mall.impl;


import com.dq.domain.goods.Product;
import com.dq.domain.goods.ShoppingBag;
import com.dq.domain.goods.ShoppingCart;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.repository.AllRepository;
import com.dq.service.ShiroService;
import com.dq.service.mall.ShoppingBagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShoppingBagServiceImpl extends AllRepository implements ShoppingBagService {

    @Autowired
    ShiroService shiroService;

    @Override
    public ShoppingBag getByCartAndProduct(ShoppingCart cart, Product product) {
        ShoppingBag bag = shoppingBagRepository.findByProductAndCart(product, cart);
        if (null == bag) {
            bag = new ShoppingBag();
            bag.setProduct(product);
            bag.setGoods(product.getGoods());
            bag.setCart(cart);
            bag.setNumber(0L);
        }
        return bag;
    }

    @Override
    public List<ShoppingBag> findByIdIn(List<Long> bagIds) {
        SysUser sysUser = sysUserService.getUser();
        ShoppingCart cart = shoppingCartRepository.getByUser(sysUser);
        return shoppingBagRepository.findByCartAndIdInAndNumberGreaterThanOrderByCreateTimeDesc(cart, bagIds, 0l);
    }
}
